export enum StatusCode {
	SUCCESS_CODE = 1,
	Business_ERR_CODE = 0,
	ERROR_CODE = 500,
	EXPIRE_CODE = 401,
	NOT_FOUND = 404,
	LOGIN_ERROR = 401
}

// @ts-ignore
const handler = {
	[StatusCode.SUCCESS_CODE]: () => {
		return true
	},
	[StatusCode.Business_ERR_CODE]: () => {
		return true
	},
	[StatusCode.ERROR_CODE]: () => {
		// @ts-ignore
		uni.showToast({
			title: '服务器错误',
			mask: true,
			icon: 'none'
		})
		return false
	},
	[StatusCode.EXPIRE_CODE]: () => {
		// @ts-ignore
		uni.showToast({
			title: '登录已过期, 请重新登录',
			mask: true,
			icon: 'none'
		})
		return false
	},
	[StatusCode.NOT_FOUND]: () => {
		// @ts-ignore
		uni.showToast({
			title: '未找到请求资源',
			mask: true,
			icon: 'none'
		})
		return false
	},
	// @ts-ignore
	[StatusCode.LOGIN_ERROR]: () => {
		// @ts-ignore
		uni.showToast({
			title: '登录失效',
			mask: true,
			icon: 'none'
		})

		// @ts-ignore
		const pages = getCurrentPages()
		let fromPath = "/" + pages[pages.length - 1].route
		setTimeout(() => {
			// @ts-ignore
			uni.redirectTo({
				url: "/pages/login/login?fromPath=" + encodeURIComponent(fromPath)
			});
		},1000)

		return false
	},
}

export const DictionaryCode = (code: StatusCode) => {
	const callback = handler[code]
	if (callback) {
		return callback()
	} else {
		console.log('未知错误');
	}
}

